package org.ucn.dmaa0914.temadesign.group2.tuiLayer;

import org.ucn.dmaa0914.temadesign.group2.modelLayer.Person;
import org.ucn.dmaa0914.temadesign.group2.controlLayer.PersonController;

/**
 * Write a description of class PersonMenu here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class PersonMenu extends Menu
{
    private int entryCreate;
    private int entryEdit;
    private int entryDelete;
    private int entrySelect;
    private int entryList;
    private int entryDetails;
    
    private PersonController controller;
    
    public PersonMenu(Menu parentMenu)
    {
        super(parentMenu, "Person Menu");
        
        entryCreate = addEntry("Create Person");
        entryEdit = addEntry("Edit Selected Person");
        entryDelete = addEntry("Delete Selected Person");
        entrySelect = addEntry("Select Person from Phone");
        entryList = addEntry("List All Persons");
        entryDetails = addEntry("Show Details about Selected Person");
        
        controller = new PersonController();
    }
    
    public void handleEntry(int entryIndex)
    {
        super.handleEntry(entryIndex);
        
        if( entryIndex == entryDelete && Application.instance.getSelectedPerson() != null )
        {
            Person p = Application.instance.getSelectedPerson();
            controller.deletePerson(p);
        }
        
        if( entryIndex == entryDetails && Application.instance.getSelectedPerson() != null )
        {
            Application.instance.setResultText(Application.instance.getSelectedPerson().makeDescription());
        }
        
        if( entryIndex == entrySelect )
        {
            String phone = prompt("Enter Phone: ");
            Application.instance.setSelectedPerson(controller.getPerson(phone));
        }
        
        if( entryIndex == entryList )
        {
            String result = "";
            for( Person p : controller.getAll() )
            {
                result += p.toString() + "\n";
            }
            Application.instance.setResultText(result);
        }
        
        if( entryIndex == entryCreate )
        {
            Person p = controller.createPerson("NoName", "NoAddress", 0, "NoCity", "NoPhone", "NoEmail");
            
            // Start editing the new person
            Application.instance.setSelectedPerson(p);
            handleEntry(entryEdit);
        }
        
        if( entryIndex == entryEdit && Application.instance.getSelectedPerson() != null )
        {
            Person p = Application.instance.getSelectedPerson();
            
            String name = prompt("Enter Name (Empty for no change): ");
            String address = prompt("Enter Address (Empty for no change): ");
            String zip = prompt("Enter Zip (Empty for no change): ");
            String city = prompt("Enter City (Empty for no change): ");
            String phone = prompt("Enter Phone (Empty for no change): ");
            String email = prompt("Enter Email (Empty for no change): ");
            
            if( name.length() == 0 )
            {
                name = p.getName();
            }
            
            if( address.length() == 0 )
            {
                address = p.getAddress();
            }
            
            if( zip.length() == 0 )
            {
                zip = String.valueOf(p.getZip());
            }
            
            if( city.length() == 0 )
            {
                city = p.getCity();
            }
            
            if( phone.length() == 0 )
            {
                phone = p.getPhone();
            }
            
            if( email.length() == 0 )
            {
                email = p.getEmail();
            }
            
            int zipInt = p.getZip();
            try
            {
                zipInt = Integer.parseInt(zip);
            }
            catch( NumberFormatException e )
            {
                //TODO: Error Message Here
            }
            controller.updatePerson(p, name, address, zipInt, city, phone, email);
        }
    }
}
